﻿using GemBox.Document;
using PdfiumViewer.Core;
using PdfiumViewer.Enums;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Xps.Packaging;
using Xceed.Words.NET;

namespace Minotaur.App.Modules.SystemSet.Views
{
    /// <summary>
    /// Interaction logic for OfficeView
    /// </summary>
    public partial class OfficeView : UserControl
    {
        Dictionary<string, string> keys = new Dictionary<string, string>()
        {
            {
               "event","事件：催办、提交退回、回收、指派、跳转"
            },
            {
               "message","站内消息、移动端消息、邮件"
            },
            {
               "sf","调用方：\r\nWeb端\r\n移动端\r\n钉钉\r\n企业微信\r\n其他第三方\r\n"
            },
            {
               "dt","数据输入方：\r\n人员\r\n组织\r\n职位\r\n字典\r\n第三方\r\n"
            },
            {
               "dy","调用业务接口\r\n人事事务\r\n离职事务\r\n转正事务\r\n调岗事务\r\n重聘事务\r\n打卡\r\n休假\r\n加班\r\n考勤\r\n"
            }
        };
        public OfficeView()
        {
            InitializeComponent();

            initPage();
        }

        private void initPage()
        {
            runs.Visibility = System.Windows.Visibility.Visible;
            runs.IsRunning = true;
            Task.Run(() =>
            {
                string wordDocName = "E:\\流程中心架构图-old.docx";
                string name = "E:\\" + System.IO.Path.GetFileNameWithoutExtension(wordDocName).Replace("-old", "").Trim() + "_new";
                string pdfFile = "E:\\" + System.IO.Path.GetFileNameWithoutExtension(wordDocName).Replace("-old", "").Trim() + ".pdf";
                string xpsFile = "E:\\" + System.IO.Path.GetFileNameWithoutExtension(wordDocName).Replace("-old", "").Trim() + ".xps";
                if (File.Exists(name))
                {
                    File.Delete(name);
                }
                if (File.Exists(pdfFile))
                {
                    File.Delete(pdfFile);
                }


                #region 通过DOCX插件替换文档中的数据

                var doc = DocX.Load(wordDocName);
                foreach (var paragraph in doc.Paragraphs)
                {
                    foreach (var item in keys)
                    {
                        try
                        {
                            paragraph.ReplaceText($"${item.Key}", item.Value);
                        }
                        catch
                        {
                        }
                    }
                }
                doc.SaveAs(name);

                #endregion

                if (File.Exists($"{name}.docx"))
                {
                    var fileStream = new FileStream($"{name}.docx", FileMode.Open, FileAccess.Read, FileShare.Read);

                    // 读取文件的 byte[]

                    byte[] bytes = new byte[fileStream.Length];

                    fileStream.Read(bytes, 0, bytes.Length);

                    fileStream.Close();

                    // 把 byte[] 转换成 Stream
                    ComponentInfo.SetLicense("FREE-LIMITED-KEY");


                    #region 生成XPS文档

                    Stream stream = new MemoryStream(bytes);

                    var pdfdocument = DocumentModel.Load(stream, LoadOptions.DocxDefault);

                    var newstream = new MemoryStream();

                    pdfdocument.Save(xpsFile, SaveOptions.XpsDefault);

                    XpsDocument xpsDocument = new XpsDocument(xpsFile, FileAccess.Read);

                    #endregion



                    #region 生成PDF文件 

                    pdfdocument.Save(newstream, SaveOptions.PdfDefault);

                    PdfDocument document = PdfDocument.Load(newstream);

                    #endregion

                    this.Dispatcher.Invoke(() =>
                    {
                        PdfiumViewer.Document = document;
                        PdfiumViewer.SetZoomMode(PdfViewerZoomMode.None);
                        xpsView.Document = xpsDocument.GetFixedDocumentSequence();
                        xpsView.FitToHeight();
                        runs.IsRunning = false;
                        runs.Visibility = System.Windows.Visibility.Collapsed;
                    });
                }
            });
        }
    }
}
